home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8805 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  57 lines

  1. Path: wkaufman.us.oracle.com!wkaufman
  2. From: wkaufman@wkaufman.us.oracle.com (William Kaufman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: REPOST:  Newbie Question -- byte order
  5. Date: 6 Mar 1996 06:52:10 GMT
  6. Organization: Oracle Corporation, Redwood Shores CA
  7. Message-ID: <4hjcmq$678@inet-nntp-gw-1.us.oracle.com>
  8. References: <4hg3u1$8d6@nnrp1.news.primenet.com>
  9. NNTP-Posting-Host: wkaufman.us.oracle.com
  10.  
  11. In article <4hg3u1$8d6@nnrp1.news.primenet.com> piersen@primenet.com (Mark Cederholm) writes:
  12. ] I have found Turbo C++ a good package for developing data conversion routines, 
  13. ] but recently I encountered a data concept not covered in their online 
  14. ] documentation (or at least I can't find it):  
  15. ] byte order (i.e. "bigendian" vs. "littleendian").
  16. ] What is it?
  17.  
  18.     Little-endian machines (like Intel/MS-DOS machines) order bytes from
  19. the least-significant to the most significant; big-endian machine (most
  20. other machines) order them the other way.
  21.  
  22.     So, for instance, assuming sizeof(short) == 2, this program
  23.  
  24.         #include <stdio.h>
  25.  
  26.         int main()
  27.         {
  28.             short x = 0x1234;
  29.             char *c = (char *)&x;
  30.  
  31.             printf("0x%02x, 0x%02x\n", c[0], c[1]);
  32.             return 0;
  33.         }
  34.  
  35. will print "0x12 0x34" on a big-endian machine, or "0x34 0x12" on a
  36. little-endian machine.
  37.  
  38.     So, if you're planning to move binary data between machines, you
  39. should pick either a big-endian or little-endian format and write out
  40. the data byte-by-byte (at least when you're on the wrong endian
  41. machine).  Most sockets libraries include ntohs(), htons(), etc., to do
  42. this for you (sockets use big-endian format).
  43.  
  44. ]  Are there any good books or Web/Gopher resources that discuss it?
  45.  
  46.     Dunno, but you could always try Alta Vista at
  47. http://altavista.digital.com/.
  48.  
  49.                                            -- Bill K.
  50.  
  51. Bill Kaufman               | " While not a master of intellect, the blatantly
  52. wkaufman@us.oracle.com     |   obvious things we take for granted never
  53.                            |   escape his keen eye! "          -- Bob Burden
  54.